// ignore_for_file: avoid_print
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base_library/index.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:safe_manage/config/my_app_provider.dart';
import 'package:safe_manage/config/my_app_routes.dart';
import 'package:provider/provider.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 强制竖屏
  SystemChrome.setPreferredOrientations(
    [
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ],
  );
  if (Platform.isAndroid) {
    SystemUiOverlayStyle style = const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    );
    SystemChrome.setSystemUIOverlayStyle(style);
  }
  KVDataUtils kvUtils = KVDataUtils.getInstance();
  await kvUtils.init();
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => MyAppProvider(),
        ),
      ],
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      builder: (context, child) {
        return ToastUtils.init(
          GestureDetector(
            onTap: () => Common.dismissKeyboard(),
            child: GetMaterialApp(
              builder: EasyLoading.init(),
              initialRoute: MyAppRoutes.mySplash,
              theme: AppThemes.themData,
              getPages: MyAppPages.getRoute,
              supportedLocales: const [Locale("zh", "CH")],
              localizationsDelegates: const [
                GlobalMaterialLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
              ],
              translationsKeys: AppTranslation.translations,
              debugShowCheckedModeBanner: false,
              defaultTransition: Transition.rightToLeftWithFade,
              onInit: () {},
            ),
          ),
        );
      },
    );
  }
}
